<?php
class Tourism_order_reviews_model extends BZ_Model
{
    protected $table   = 'tourism_order_reviews';
    protected $table_2 = 'tourism_order';
    protected $table_3 = 'tourism_order_product';
    protected $table_4 = 'tourism_reviews_reply';

    /**
     * 产品评论数量
     * @param unknown $params
     */
    public function total($params = array())
    {
        $this->db->select('COUNT(*) AS count');
        $this->db->from($this->table . ' AS tourism_order_reviews');
        $this->db->join($this->table_2 . ' AS tourism_order', 'tourism_order.order_id = tourism_order_reviews.order_id');
        $this->db->join($this->table_3 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_reviews.order_product_id');
        $this->db->join($this->table_4 . ' AS tourism_reviews_reply', 'tourism_reviews_reply.review_id = tourism_order_reviews.id', 'left');
        if (!empty($params['seller_uid'])) {
            $this->db->where('tourism_order.seller_uid', $params['seller_uid']);
        }
        if (!empty($params['goods_name'])) {
            $this->db->like('tourism_order_reviews.goods_name', $params['goods_name']);
        }
        if (!empty($params['user_name'])) {
            $this->db->where('tourism_order_reviews.user_name', $params['user_name']);
        }
        $result = $this->db->get();
        return $result->num_rows();
    }

    /**
     * 产品评论列表
     * @param unknown $page_num
     * @param unknown $num
     * @param unknown $params
     */
    public function page_list($page_num, $num, $params = array())
    {
        $this->db->select('tourism_order_reviews.*,tourism_order_product.goods_attr_id,tourism_order_product.goods_image,tourism_order_product.number, tourism_reviews_reply.reply_user_name');
        $this->db->from($this->table . ' AS tourism_order_reviews');
        $this->db->join($this->table_2 . ' AS tourism_order', 'tourism_order.order_id = tourism_order_reviews.order_id');
        $this->db->join($this->table_3 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_reviews.order_product_id');
        $this->db->join($this->table_4 . ' AS tourism_reviews_reply', 'tourism_reviews_reply.review_id = tourism_order_reviews.id', 'left');
        if (!empty($params['seller_uid'])) {
            $this->db->where('tourism_order.seller_uid', $params['seller_uid']);
        }
        if (!empty($params['goods_name'])) {
            $this->db->like('tourism_order_reviews.goods_name', $params['goods_name']);
        }
        if (!empty($params['user_name'])) {
            $this->db->where('tourism_order_reviews.user_name', $params['user_name']);
        }
        $this->db->order_by('tourism_order.order_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    /**
     * 根据当前产品ID，获取当前的评论详情，一个。
     * @param unknown $params
     */
    public function findByReviewId($params=array())
    {
        $this->db->select('tourism_order_reviews.*, tourism_order_product.goods_attr_id, tourism_order_product.goods_image, tourism_order_product.number,tourism_order_product.refund_num, tourism_order_product.market_price, tourism_order_product.shop_price,tourism_order_product.supply_price, tourism_reviews_reply.id AS reply_id, tourism_reviews_reply.reply_user_name, tourism_reviews_reply.reply_content, tourism_reviews_reply.created_at AS reply_time');
        $this->db->from($this->table.' AS tourism_order_reviews');
        $this->db->join($this->table_3 . ' AS tourism_order_product', 'tourism_order_product.order_product_id = tourism_order_reviews.order_product_id');
        $this->db->join($this->table_4 . ' AS tourism_reviews_reply', 'tourism_order_reviews.id = tourism_reviews_reply.review_id', 'LEFT');
    
        $this->db->where('tourism_order_reviews.id', $params['review_id']);
        $this->db->group_by('tourism_order_reviews.id');
        return $this->db->get();
    }
    
    public function reviewsReply($params=array())
    {
        $data = array(
            'review_id'       => $params['review_id'],
            'reply_user_name' => $params['reply_user_name'],
            'reply_content'   => $params['reply_content'],
        );
        $this->db->insert($this->table_4, $data);
        return $this->db->insert_id();
    }
    
    public function insertBatchReviews($params)
    {
        $data = array();
        foreach ($params as $value) {
            $data[] = array(
                'order_product_id'=> $value['order_product_id'],
                'order_id'        => $value['order_id'],
                'goods_attr_id'   => $value['goods_attr_id'],
                'goods_image'     => $value['goods_image'],
                'attribute_value' => $value['attribute_value'],
                'goods_name'      => $value['goods_name'],
                'uid'             => $value['uid'],
                'user_name'       => $value['user_name'],
                'user_content'    => $value['user_content'],
                'status'          => $value['status'],
                'review_score'    => $value['review_score'],
            );
        }
        return $this->db->insert_batch($this->table, $data);
    }
}